package com.neo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
	
    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+"，this is first messge";
        //return "hello "+name+"，this is producer 2  send first messge";
    }

    @PostMapping("login")
    @ApiOperation(value = "销售端登录接口")
    public Object login(@RequestBody UserAuthReq body, HttpServletRequest request) {
        String username = body.getUsername();
        String password = body.getPassword();
        if (username == null || password == null) {
            return ResponseUtil.badArgument();
        }

        LitemallUser user = userService.queryByMobile(username);
        if (null == user) {
            return ResponseUtil.fail(WxResponseCode.AUTH_INVALID_ACCOUNT, "账号不存在");
        }
        if (user.getStatus() == UserStatusEnums.SUSPEND.code()) {
            return ResponseUtil.fail(WxResponseCode.AUTH_INVALID_ACCOUNT, "该用户无登录权限");
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (!encoder.matches(password, user.getPassword())) {
            return ResponseUtil.fail(WxResponseCode.AUTH_INVALID_ACCOUNT, "账号密码不对");
        }
        List<LitemallStoreUser> storeUserList = storeUserService.findByUserId(user.getId());
        if (storeUserList == null || storeUserList.isEmpty()) {
            logger.info("login|收银台账号密码登录|账号异常,账号下没有门店,userId:" + user.getId());
            return ResponseUtil.fail(WxResponseCode.AUTH_INVALID_ACCOUNT, "该用户不是代理");
        }
        Integer storeId = storeUserList.get(0).getStoreId();
        LitemallStore store = storeService.findById(storeId);
        if (null == store) {
            logger.info("login|收银台账号密码登录|store不存在,storeId:" + store.getId());
            return ResponseUtil.fail(WxResponseCode.USER_NOT_LOGIN_AUTHORITY, "该用户无登录权限");
        }
        Short status = store.getStatus();
        if (null == status) {
            return ResponseUtil.fail("门店状态异常");
        }
        if (StoreStatusEnums.INVISIBLE.code() == status) {
            return ResponseUtil.fail("门店已经关闭");
        }
        if (store.getSaleFlag() != 1) {
            return ResponseUtil.fail(WxResponseCode.USER_NOT_LOGIN_AUTHORITY, "该用户无登录权限");
        }
        // 更新登录情况
        user.setLastLoginTime(LocalDateTime.now());
        user.setLastLoginIp(IpUtil.getIpAddr(request));

        if (userService.updateById(user) == 0) {
            return ResponseUtil.updatedDataFailed();
        }

        StoreStaff storeStaff = new StoreStaff();
        storeStaff.setStoreId(storeId);
        storeStaff.setUserId(user.getId());
        storeStaff.setRole(storeUserList.get(0).getRole());
        // token
        String token = StoreUserTokenManager.generateToken(storeStaff);

        // 查询代理
        Map<Object, Object> result = new HashMap<Object, Object>();
        result.put("token", token);
        result.put("roleId", storeUserList.get(0).getRole());
        result.put("userId", user.getId());
        if (store != null) {
            result.put("rank", store.getRank());
        }

        logger.info("login|密码登录|username:" + username + "|userId:" + user.getId() + "|roleId:" + storeUserList.get(0).getRole());
        return ResponseUtil.ok(result);
    }
}